package com.zwj.interview.BFS;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @Author: Zwj
 * @Date: 2023/2/2 14:13
 */
public class 二叉树中序遍历 {

    //中序遍历遵循:左节点  当前节点  右节点
    public List<Integer> inOrder(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        List<Integer> result = new ArrayList<>();
        while (stack.size() > 0 || root != null) {
            //先将左边压入栈中
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
            //整个左子树走完了，那么就弹出最左边的放入结果集
            TreeNode cur = stack.pop();
            result.add(cur.val);
            root = cur.right;
        }
        return result;
    }
}